package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.constant.CrowdContant;
import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.service.api.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class AdminHandler {

    @Autowired
    private AdminService adminService;


    @RequestMapping("/admin/update.html")
    public String update(Admin admin,@RequestParam("pageNum") Integer pageNum,@RequestParam("keyword") String keyword){

        adminService.update(admin);
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }
    @RequestMapping("admin/to/edit/page.html")
    public String getAdminByAdminId(
            @RequestParam("adminId") Integer adminId,
            ModelMap modelMap)
            {

        Admin admin=adminService.getAdminById(adminId);

        modelMap.addAttribute("admin",admin);

        return "admin-edit";


    }

    @RequestMapping("/admin/save.html")
    public String doAdd(Admin admin){
        System.out.println(admin);
        adminService.add(admin);
        return "redirect:/admin/get/page.html?pageNum="+Integer.MAX_VALUE;
    }

    @RequestMapping("/admin/remove/{adminId}/{pageNum}/{keyword}.html")
    public String doRemove(
            @PathVariable("adminId") Integer adminId,
            @PathVariable( "pageNum") Integer pageNum,
            @PathVariable( "keyword") String keyword){

        adminService.remove(adminId);
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }

    @PreAuthorize("hasRole('经理操作者')")
    @RequestMapping("admin/get/page.html")
    public String doPage(
//            defaultValue指定默认值请求参数没有时使用默认值
            @RequestParam(value = "keyword",defaultValue = "") String keyword,
            //默认值是1
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            ModelMap modelMap

    ){
        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);

        System.out.println(pageInfo.getList());

        modelMap.addAttribute(CrowdContant.ATTR_NAME_PAGE_INFO,pageInfo);

        return "admin-page";
    }

    @RequestMapping("/admin/do/logout.html")
    public String doLoginOut(HttpSession session){

        session.invalidate();
        return "redirect:/admin/login.html";
    }

    @RequestMapping("/admin/do/login.html")
    public String adminLogin(String loginAcct, String userPswd, HttpSession session){

        Admin admin = adminService.getAdminByloginAcct(loginAcct, userPswd);

        session.setAttribute(CrowdContant.ATTR_NAME_ADMIN,admin);

        return "redirect:/admin/to/main/page.html";
    }


//    @RequestMapping("/security/do/login.html")
//    public String securityLogin(){
//
//
//        return "redirect:/admin/to/main/page.html";
//    }
}
